<?php
declare(strict_types=1);

namespace app\model;

use think\Model;

class UserPackage extends Model
{
    protected $autoWriteTimestamp = true;

    // 关联用户
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    // 关联商家
    public function merchant()
    {
        return $this->belongsTo(Merchant::class);
    }

    // 关联会员套餐
    public function package()
    {
        return $this->belongsTo(MemberPackage::class, 'package_id');
    }

    // 是否有效
    public function isValid(): bool
    {
        return $this->valid_time > time();
    }

    // 获取剩余天数
    public function getRemainingDays(): int
    {
        if (!$this->isValid()) {
            return 0;
        }
        return ceil(($this->valid_time - time()) / 86400);
    }

    // 获取有效期文字
    public function getValidTimeTextAttr()
    {
        if (!$this->isValid()) {
            return '已过期';
        }
        return date('Y-m-d H:i:s', $this->valid_time);
    }
} 